package com.booking.controller;

import com.booking.common.dto.ClientResp;
import com.booking.common.dto.PageReq;
import com.booking.common.dto.PageResp;
import com.booking.controller.model.req.grouping.GroupingCreateReq;
import com.booking.controller.model.req.grouping.GroupingOrderFollowReq;
import com.booking.controller.model.req.grouping.GroupingQueryReq;
import com.booking.controller.model.req.grouping.GroupingUpdateReq;
import com.booking.controller.model.req.order.OrderPageQueryReq;
import com.booking.controller.model.res.GroupingDetail;
import com.booking.controller.model.res.GroupingOrderDetail;
import com.booking.service.GroupingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Qikai.Lin
 * @date 2023/4/23
 */
@Slf4j
@RestController
@RequestMapping("/api/grouping")
public class GroupingController {

    @Autowired
    private GroupingService groupingService;

    //查询所有拼团信息
    @PostMapping("/getAll")
    public ClientResp<PageResp<GroupingDetail>> getAll(@RequestBody @Valid PageReq<GroupingQueryReq> request) {
        return ClientResp.success(groupingService.getAllGroupingInfo(request));
    }
    //查询拼团详情
    @PostMapping("/detail")
    public ClientResp<GroupingDetail> getDetail(@RequestParam Long groupingId) {
        return ClientResp.success(groupingService.getGroupingInfoDetail(groupingId));
    }
    //查询所有拼团订单
    @PostMapping("/order/getAll")
    public ClientResp<PageResp<GroupingOrderDetail>> getGroupingOrderPageByUserId(@RequestBody @Valid PageReq<OrderPageQueryReq> request) {
        return ClientResp.success(groupingService.getGroupingOrderPage(request));
    }
    //查询拼团订单详情
    @PostMapping("/order/detail")
    public ClientResp<GroupingOrderDetail> getGroupingOrderDetail(@RequestParam Long orderId) {
        return ClientResp.success(groupingService.getGroupingOrderDetail(orderId));
    }
    //创建拼团
    @PostMapping("/create")
    public ClientResp<String> create(@RequestBody @Valid GroupingCreateReq request) {
        // 创建拼团
        Long groupingId = groupingService.createGrouping(request);
        // 拼团者参与拼团
        GroupingOrderFollowReq followReq = new GroupingOrderFollowReq();
        followReq.setGroupingId(groupingId);
        followReq.setUserId(request.getUserId());
        groupingService.followGrouping(followReq);
        return ClientResp.success();
    }
    //更新拼团
    @PostMapping("/update")
    public ClientResp<String> update(@RequestBody @Valid GroupingUpdateReq request) {
        groupingService.updateGrouping(request);
        return ClientResp.success();
    }

    //参与拼团
    @PostMapping("/follow")
    public ClientResp<String> follow(@RequestBody @Valid GroupingOrderFollowReq request) {
        groupingService.followGrouping(request);
        return ClientResp.success();
    }

    //删除民宿
    @PostMapping("/delete")
    public ClientResp<String> delete(@RequestParam Long groupingId) {
        groupingService.deleteGrouping(groupingId);
        return ClientResp.success();
    }
    //取消拼团
    @PostMapping("/order/cancel")
    public ClientResp<String> cancel(@RequestParam Long orderId) {
        groupingService.cancelGroupingOrder(orderId);
        return ClientResp.success();
    }

    //支付拼团订单定金
    @PostMapping("/order/bookingPay")
    public ClientResp<String> bookingPay(@RequestParam Long orderId) {
        groupingService.payGroupingOrderBooking(orderId);
        return ClientResp.success();
    }
    //支付拼团订单
    @PostMapping("/order/pay")
    public ClientResp<String> pay(@RequestParam Long orderId) {
        groupingService.payGroupingOrder(orderId);
        return ClientResp.success();
    }
    @PostMapping("/order/finish")
    public ClientResp<String> finish(@RequestParam Long orderId) {
        groupingService.finishOrder(orderId);
        return ClientResp.success();
    }
}
